package com.situ.hospital.servlet;

import com.situ.hospital.pojo.Auth;
import com.situ.hospital.pojo.query.AuthQuery;
import com.situ.hospital.service.IAuthService;
import com.situ.hospital.service.impl.AuthServiceImpl;
import com.situ.hospital.util.JSONUtil;
import com.situ.hospital.util.MyBeanUtil;
import com.situ.hospital.util.PageResult;
import com.situ.hospital.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/auth")
public class AuthServlet extends HttpServlet {

    private IAuthService authService = new AuthServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "updateStatus":
                updateStatus(req,resp);
                break;
        }
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        authService.updateStatus(Integer.parseInt(id),Integer.parseInt(status));
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.update");
        Auth auth = MyBeanUtil.copyToBean(req, Auth.class);
        authService.Update(auth);
        JSONUtil.toJSON(resp, Result.ok("修改成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.selectById");
        String id = req.getParameter("id");
        Auth auth = authService.selectById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok(auth));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.add");
        Auth auth = MyBeanUtil.copyToBean(req, Auth.class);
        authService.add(auth);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        authService.deleteAll(ids);
        JSONUtil.toJSON(resp,Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.deleteById");
        String id = req.getParameter("id");
        authService.deleteById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthServlet.selectByPage");
        AuthQuery authQuery = MyBeanUtil.copyToBean(req,AuthQuery.class);
        PageResult pageResult = authService.selectByPage(authQuery);
        JSONUtil.toJSON(resp,pageResult);
    }
}
